<template>
	<view>
		<uni-popup ref="popupRef" type="center">
			<view class="popup">
				<uni-icons @click="popupRef.close()" class="close" type="closeempty" size="24" color="#AEAEAE"></uni-icons>
				<view class="uimg">
					<image src="/static/vip.png" mode=""></image>
				</view>
				<view class="title">成为稻浪会员</view>
				<view class="pop_btn" @click="govip">加入稻浪同歌</view>
			</view>
		</uni-popup>
		<!-- 修改地址 -->
		<uni-popup ref="popupAddress" type="center">
			<view class="globalPopup">
				<uni-icons @click="close" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">修改地址</view>
				<view class="subtitle">原定地址：{{ dataInfo?.addressInfo }}</view>
				<view class="block_1">
					<view class="tit">地图导航</view>
					<view class="input_flex flex" @click="selectDW">
						<view class="input" v-if="addressObj.address">
							{{ addressObj.address }}
						</view>
						<view class="flex input" v-else>
							<uni-icons type="location-filled" size="24" color="#75613E"></uni-icons>
							<view class="t1">请选择</view>
						</view>
					</view>
				</view>
				<view class="block_1" style="margin-bottom: 30rpx">
					<view class="tit">现场地址</view>
					<view class="input">
						<textarea v-model="addressObj.addressInfo" placeholder-class="input_class" type="text" placeholder="请输入" maxlength="30" />
					</view>
				</view>

				<view class="btns flex_jc_sb">
					<view class="bt b1" @click="close">取消</view>
					<view class="bt b2" @click="confirmEditAddress">确认</view>
				</view>
			</view>
		</uni-popup>
		<!-- 审核弹窗 -->
		<uni-popup ref="popupReview" type="center">
			<view class="globalPopup">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">活动审核</view>
				<view class="shenhe">
					<view class="tit_right">
						<radio-group @change="radioChange">
							<label class="radio" style="color: #000">
								<radio style="transform: scale(0.7)" color="#B19561" value="已通过" />
								通过
							</label>
							<label class="radio" style="color: #000; margin-left: 50rpx">
								<radio style="transform: scale(0.7)" color="#B19561" value="已驳回" />
								驳回
							</label>
						</radio-group>
					</view>
					<view class="popup_textarea" v-if="fenshe.shenhe.verifyStatus == '已驳回'">
						<textarea v-model="fenshe.shenhe.verifyDesc" placeholder="请输入说明" placeholder-class="input_text" maxlength="180" confirm-type="done"></textarea>
					</view>
				</view>
				<view class="btns flex_jc_sb">
					<view class="bt b1" @click="close()">取消</view>
					<view class="bt b2" @click="handleSHENHE">确认</view>
				</view>
			</view>
		</uni-popup>
		<!-- 活动推荐到热门 -->
		<uni-popup ref="popupApply" type="center">
			<view class="globalPopup">
				<uni-icons @click="close" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">活动推荐到热门</view>
				<view class="shenhe" style="margin-top: 30rpx">
					<view class="popup_textarea">
						<textarea v-model="shenqingObj.hotApplyDesc" placeholder="请输入推荐原因" placeholder-class="input_text" maxlength="180" confirm-type="done"></textarea>
					</view>
				</view>
				<view class="btns flex_jc_sb">
					<view class="bt b1" @click="close">取消</view>
					<view class="bt b2" @click="handleTUIJIAN">确认</view>
				</view>
			</view>
		</uni-popup>
		<!-- 延迟时间弹窗 -->
		<uni-popup ref="popupTime" type="center">
			<view class="globalPopup">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">延迟时间</view>
				<view class="time_s" v-if="dataInfo?.beginTime">
					<view class="t1">原定时间：{{ $formatDateTime(dataInfo.beginTime) }}</view>
					<view class="">
						<uni-datetime-picker :start="Date.now()" v-model="fenshe.timeObj.beginTime" type="datetime" :hide-second="true" />
					</view>
				</view>
				<view class="btns flex_jc_sb">
					<view class="bt b1" @click="close()">取消</view>
					<view class="bt b2" @click="handleRTime">确认</view>
				</view>
			</view>
		</uni-popup>
		<!-- 更新群码 -->
		<uni-popup ref="popupImg" type="center">
			<view class="globalPopup">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">更新群码</view>
				<view class="upload">
					<image :src="dataInfo.imgQr" mode="aspectFill"></image>
					<view class="ads" @click="chooseFENGMIAN(1)">重新上传</view>
				</view>
			</view>
		</uni-popup>
		<!-- 上传合影 -->
		<uni-popup ref="popupPhoto" type="center">
			<view class="globalPopup">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">上传合影</view>
				<view class="upload1" v-if="dataInfo.endImg">
					<image :src="dataInfo.endImg" mode="aspectFill"></image>
					<view class="adsts">重新上传</view>
				</view>
				<view class="upload1 flex_jc_cent" v-else>
					<image src="https://www.daolangtongge.com/read-book-api/profile/mini_app/img/upbcg.png" mode=""></image>
					<view class="ads flex_jc_cent" @click="chooseFENGMIAN(2)">
						<view class="">
							<image src="/static/bxj.png" mode=""></image>
							<view class="" style="margin-top: 10rpx">上传活动照片</view>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 导出弹窗 -->
		<uni-popup ref="popupDown" type="center">
			<view class="globalPopup">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">确认导出</view>
				<view class="content">是否要导出当前活动？</view>
				<view class="btns flex_jc_sb">
					<view class="bt b1" @click="close()">取消</view>
					<view class="bt b2">确认</view>
				</view>
			</view>
		</uni-popup>

		<!-- 报名成功弹窗 -->
		<uni-popup ref="popupYes" type="center">
			<view class="popup">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title">报名成功！</view>
				<view class="subtitle">恭喜您报名成功！请扫码加入活动群</view>
				<view class="img">
					<image :src="dataInfo.imgQr" mode="" :show-menu-by-longpress="true"></image>
				</view>
				<view class="pop_btn">
					<view class="bt b2">长按二维码保存</view>
				</view>
			</view>
		</uni-popup>
		<!-- 取消报名弹窗 -->
		<uni-popup ref="popupNo" type="center">
			<view class="popup p1">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title1">取消报名</view>
				<view class="pop_content" style="line-height: 50rpx;">
					<view style="margin-bottom: 20rpx; text-align: center">为共同守护活动资源，请知晓：</view>
					<view>• 提前 ≥72小时取消：稻穗值完好无损</view>
					<view>• 72小时内取消：将按规则扣除相应稻穗值4-10分</view>
					<view>• 活动缺席或未签到：扣除稻穗值20分</view>
				</view>
				<view class="pop_btnflex flex_jc_sb">
					<view class="btn b2" @click="handleQUXIAO">确认取消</view>
					<view class="btn b2" @click="close()">放弃取消</view>
				</view>
			</view>
		</uni-popup>
		<!-- 申请主持人弹窗 -->
		<uni-popup ref="popupZHUCHI" type="center">
			<view class="popup p1">
				<uni-icons @click="close()" type="closeempty" size="20" class="close" color="#AEAEAE"></uni-icons>
				<view class="title1">申请成为主持人</view>
				<view class="pop_content" style="text-align: center">是否确认成为主持人？</view>
				<view class="pop_btnflex flex_jc_sb">
					<view class="btn b1" @click="close()">取消</view>
					<view class="btn b2" @click="applyZHUCHI">确认</view>
				</view>
			</view>
		</uni-popup>
		<!-- 签到成功 -->
		<uni-popup ref="popupQIANDAO" type="center">
			<view class="popup">
				<view class="title">签到成功！</view>
				<view class="pop_content" style="color: #b19561">祝您共耕愉快，收获满仓！</view>
				<view class="pop_btn">
					<view class="bt b2" style="padding: 0rpx 60rpx" @click="close()">我知道了</view>
				</view>
			</view>
		</uni-popup>
		<!-- 签到失败 -->
		<uni-popup ref="popupQIANDAOSHIBAI" type="center">
			<view class="popup">
				<view class="title">不可签到</view>
				<view class="pop_content" style="color: #b19561; padding: 0%">
					<view class="">快到了！</view>
					<view class="">进入活动地点{{ configData.activitySignInDistance / 1000 }}公里范围内就可以签到啦～</view>
				</view>
				<view class="pop_btn">
					<view class="bt b2" style="padding: 0rpx 60rpx" @click="close()">我知道了</view>
				</view>
			</view>
		</uni-popup>

		<!-- 底部按钮区域 -->
		<view class="btn_zw"></view>
		<view class="bottom_btns_page">
			<!-- 驳回提示 -->
			<view class="" v-if="!userStatus.isEnded">
				<view class="ttt" v-if="userStatus.isCreator && userStatus.isRejected">已驳回：{{ dataInfo.verifyDesc }}</view>
				<!-- 人数提示 -->
				<view class="ddd" v-if="!userStatus.isSignedUp && userStatus.isFull">人员已满，点击"候补报名"可接收候补提醒</view>
				<view class="ddd" v-if="userStatus.isBackupSign">候补成功，请您耐心等待</view>
				<view class="ddd" v-if="userStatus.isCreator && userStatus.isPending">等待社长审核</view>

				<view class="ddd" v-if="userStatus.isGroupLeader && dataInfo.hotApply == '1' && dataInfo.hotVerifyStatus == '待审核'">
					推荐申请原因：{{ dataInfo.hotApplyDesc }}。待平台审核
				</view>
				<view class="ttt" v-if="userStatus.isGroupLeader && dataInfo.hotApply == '1' && dataInfo.hotVerifyStatus == '已驳回'">推荐驳回：{{ dataInfo.hotVerifyDesc }}</view>
			</view>
			<view class="cont">
				<!-- 审核状态提示 -->
				<!-- 左侧操作按钮 -->
				<view class="cont_left flex">
					<!-- v-show="show" -->
					<!-- 更多 -->
					<view class="itemimg" v-if="userStatus.isGroupLeader && !userStatus.isEnded && userStatus.isApproved" @click="fenshe.isshowBtn = !fenshe.isshowBtn">
						<image src="/static/gd.png" mode=""></image>
						<view class="tt">更多</view>
						<view class="dropdown-menu" v-show="fenshe.isshowBtn">
							<!-- 箭头 -->
							<view class="arrow"></view>
							<!-- 菜单内容（添加ref用于获取实际高度） -->
							<view class="menu-content">
								<view class="" v-for="(item, index) in fenshe.menuOptions" :key="index" @click="handleMenuSelect(item.value)">
									<view class="menu-item" v-if="item.isshow">
										<view class="texts">
											{{ item.label }}
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>

					<!-- 转发 -->
					<view class="itemimg">
						<image src="/static/zf.png" mode=""></image>
						<button class="zf_btns" open-type="share"></button>
						<view class="tt">转发</view>
					</view>

					<!-- 推荐 -->
					<view
						@click="popupApply.open()"
						class="itemimg"
						v-if="userStatus.isGroupLeader && userStatus.isApproved && !userStatus.isEnded && dataInfo.hot == 0 && dataInfo.hotVerifyStatus != '待审核'"
					>
						<!-- <image v-if="dataInfo.hotApply == '1'" src="/static/tj1.png" mode=""></image> -->
						<image src="/static/tj.png" mode=""></image>
						<view class="tt">推荐</view>
					</view>
				</view>
				<!-- 右侧主要操作按钮 -->
				<view class="">
					<!-- 创建者操作 -->
					<view class="flex" v-if="userStatus.isCreator && userStatus.isRejected">
						<view class="btn b1" style="margin-right: 20rpx" @click="handleRemove">删除</view>
						<view class="btn b2" @click="$pageGo(`/mypage/activity/activity?sendType=setList&&id=${dataInfo.id}`)">修改</view>
					</view>
					<!-- 社长审核 -->
					<view v-if="userStatus.isGroupLeader && userStatus.isPending" @click="popupReview.open()" class="btns">审核</view>
					<!-- 活动结束 -->
					<view v-if="userStatus.isEnded">
						<view class="btns b1">活动已结束</view>
					</view>
					<!-- 活动进行中 -->
					<view v-else>
						<!-- 已报名用户操作 -->
						<view v-if="userStatus.isSignedUp">
							<!-- 正常报名用户 -->
							<view class="flex" v-if="userStatus.isNormalSign">
								<!-- 主持人相关 -->
								<view v-if="!userStatus.hasHost" class="btn b2" style="margin-right: 20rpx" @click="popupZHUCHI.open()">成为主持人</view>
								<view v-else>
									<view v-if="userStatus.isHost" class="btn b2" style="margin-right: 20rpx" @click="closeZHUCHI">取消主持</view>
									<view v-else class="btn b1" style="margin-right: 20rpx">主持人已定</view>
								</view>

								<!-- 取消报名/签到 -->
								<view v-if="userStatus.isCloseSing && !userStatus.isTime && dataInfo.activitySign.sign == '0'" class="btn b2" @click="popupNo.open()">
									取消报名
								</view>
								<view v-if="userStatus.isSing">
									<!-- 签到逻辑需要根据实际业务补充 -->
									<view class="btn b1" v-if="dataInfo.activitySign.sign == '1'">已签到</view>
									<view class="btn b2" v-else @click="handleSignIn">签到</view>
									<!-- <view class="btn b1">不可签到</view> -->
								</view>
							</view>

							<!-- 候补报名用户 -->
							<view v-else>
								<view class="btn b2">已候补</view>
							</view>
						</view>

						<!-- 未报名用户操作 -->
						<view v-else>
							<view class="" v-if="userStatus.isOpen">
								<view class="btns b1">活动已开始，报名截止</view>
							</view>
							<view class="" v-else>
								<view v-if="userStatus.isApproved && !userStatus.isFull" @click="handleBAOMING" class="btns">立即报名</view>
								<view v-if="userStatus.isApproved && userStatus.isFull" @click="handleHOUBUBAOMING" class="btns">候补报名</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script setup>
import { ref, watch, reactive, defineEmits, computed, getCurrentInstance } from 'vue';
import { activitySignUp, activitySignHb, activitySignCancel, activitySignEmceeApply, activitySignEmceeCancel, removeActivity, activitySignIn } from '@/api/activity.js';
import { getUser } from '@/api/user.js';
import { groupVerify, groupDelayTime, groupUpdateImgQr, groupUpdateEndImg, groupApplyHot, activityUpdateAddress } from '@/api/branch/factivity.js';
import { toast, showConfirm } from '@/utils/common.js';
import { chooseAndUploadImage } from '@/utils/upload';
const { proxy } = getCurrentInstance();
const emit = defineEmits(['refesh_info']);
const props = defineProps({
	dataInfo: {
		type: Object,
		required: true
	}
});

// refs
const popupReview = ref(null);
const popupYes = ref(null);
const popupNo = ref(null);
const popupZHUCHI = ref(null);
const popupQIANDAO = ref(null);
const popupQIANDAOSHIBAI = ref(null);

const popupDown = ref(null);
const popupTime = ref(null);
const popupImg = ref(null);
const popupPhoto = ref(null);
const popupApply = ref(null);
const popupRef = ref(null);
const popupAddress = ref(null);

const close = () => {
	setTimeout(() => {
		popupReview.value.close();
		popupYes.value.close();
		popupNo.value.close();
		popupZHUCHI.value.close();
		popupQIANDAO.value.close();
		popupQIANDAOSHIBAI.value.close();
		popupDown.value.close();
		popupTime.value.close();
		popupImg.value.close();
		popupPhoto.value.close();
		popupApply.value.close();
		popupAddress.value.close();
	}, 300);
};
// 状态
const isa = ref(false);
const isb = ref(false);
const userID = ref(0);
const vip = ref(0);
import { getConfig } from '@/api/login.js';
const configData = reactive({});

// 计算属性
// 判断时间是否在指定小时数内
const isWithinHours = (targetTime, hours = 10) => {
	const iosSafeDateStr = targetTime?.replace(' ', 'T');
	if (!iosSafeDateStr) return false;
	const target = new Date(iosSafeDateStr).getTime();
	const now = new Date().getTime();
	const diff = target - now;
	const hoursInMs = hours * 3600000; // 小时转毫秒
	return Math.abs(diff) <= hoursInMs;
};

// 定义时间范围类型
/**
 * @typedef {Object} TimeRange
 * @property {number} [days] - 天数
 * @property {number} [hours] - 小时数
 * @property {number} [minutes] - 分钟数
 */
/**
 * 判断当前时间是否在目标时间的前后指定范围内
 * @param {string|Date} targetTime - 目标时间
 * @param {Object} options - 配置选项
 * @param {TimeRange} options.before - 目标时间前的时间范围
 * @param {TimeRange} options.after - 目标时间后的时间范围
 * @returns {boolean}
 */
const isAroundTime = (targetTime, options = {}) => {
	const { before = {}, after = {} } = options;

	// 处理目标时间
	let targetTimestamp;

	if (targetTime instanceof Date) {
		targetTimestamp = targetTime.getTime();
	} else if (typeof targetTime === 'string') {
		const iosSafeDateStr = targetTime.replace(' ', 'T');
		targetTimestamp = new Date(iosSafeDateStr).getTime();
	} else if (typeof targetTime === 'number') {
		targetTimestamp = targetTime;
	} else {
		return false;
	}

	if (isNaN(targetTimestamp)) {
		return false;
	}

	const now = Date.now();

	// 计算时间范围（毫秒）
	const calculateMs = (range) => {
		const { days = 0, hours = 0, minutes = 0 } = range;
		return days * 24 * 60 * 60 * 1000 + hours * 60 * 60 * 1000 + minutes * 60 * 1000;
	};

	const beforeMs = calculateMs(before);
	const afterMs = calculateMs(after);

	// 判断当前时间是否在目标时间的前后范围内
	return now >= targetTimestamp - beforeMs && now <= targetTimestamp + afterMs;
};
const isTimeExceeded = (targetTime) => {
	// 处理目标时间
	let targetTimestamp;

	if (targetTime instanceof Date) {
		targetTimestamp = targetTime.getTime();
	} else if (typeof targetTime === 'string') {
		const iosSafeDateStr = targetTime.replace(' ', 'T');
		targetTimestamp = new Date(iosSafeDateStr).getTime();
	} else if (typeof targetTime === 'number') {
		targetTimestamp = targetTime;
	} else {
		return false;
	}

	if (isNaN(targetTimestamp)) {
		return false;
	}

	const now = Date.now();
	return now > targetTimestamp;
};
const shouldShow = (activityTime) => {
	// 活动已开始 → 不显示
	if (isTimeExceeded(activityTime)) {
		return false;
	}
	// 活动开始前1小时内 → 不显示
	if (isAroundTime(activityTime, { before: { hours: 1 } })) {
		return false;
	}
	return true;
};
// 用户身份状态
const userStatus = computed(() => {
	return {
		isCreator: isa.value, // 活动创建者
		isGroupLeader: isb.value, // 社长
		isSignedUp: !!props.dataInfo.activitySign, // 已报名
		isNormalSign: props.dataInfo.activitySign?.supplement === '0', // 正常报名
		isBackupSign: props.dataInfo.activitySign?.supplement === '1', // 候补报名
		isFull: props.dataInfo.signUserList?.length >= props.dataInfo.maxCount, // 人数已满
		isSign: props.dataInfo.sign === '1', // 活动设置了签到
		isHost: props.dataInfo.emceeUser?.id === userID.value, // 是主持人
		hasHost: !!props.dataInfo.emceeUser, // 已有主持人
		isApproved: props.dataInfo.verifyStatus === '已通过', // 已审核通过
		isRejected: props.dataInfo.verifyStatus === '已驳回', // 已驳回
		isPending: props.dataInfo.verifyStatus === '待审核', // 待审核
		isEnded: props.dataInfo.end === '1', // 已结束
		isSing: isAroundTime(props.dataInfo.beginTime, { before: { minutes: configData.activitySignInMinuteBefore }, after: { minutes: configData.activitySignInMinuteAfter } }), //能不能签到
		isCloseSing: shouldShow(props.dataInfo.beginTime), // 能不能取消报名
		isTime: isAroundTime(props.dataInfo.beginTime, { before: { minutes: configData.activitySignInMinuteBefore } }),
		isOpen: isTimeExceeded(props.dataInfo.beginTime) //活动是不是开始了
	};
});
// 监听数据变化
watch(
	() => props.dataInfo,
	(newVal) => {
		if (newVal && Object.keys(newVal).length > 0) {
			getUser({}).then((res) => {
				if (res.code === 200) {
					userID.value = res.data.user.id;
					vip.value = res.data.user.vip;
					isa.value = props.dataInfo.createUser?.id === res.data.user.id;
					isb.value = props.dataInfo.createUser?.groupId === res.data.user.groupId && res.data.user.vipType === 3;

					fenshe.menuOptions[0].isshow = !isTimeExceeded(props.dataInfo.beginTime) && props.dataInfo.verifyStatus == '已通过';
					fenshe.menuOptions[1].isshow = !isTimeExceeded(props.dataInfo.beginTime) && props.dataInfo.verifyStatus == '已通过';
					fenshe.menuOptions[2].isshow = props.dataInfo.verifyStatus == '已通过';
					fenshe.menuOptions[3].isshow = isTimeExceeded(props.dataInfo.beginTime) && props.dataInfo.verifyStatus == '已通过';
					fenshe.menuOptions[4].isshow = props.dataInfo.verifyStatus == '已通过';
					fenshe.menuOptions[5].isshow = props.dataInfo.lineType == '2' && !isTimeExceeded(props.dataInfo.beginTime) && props.dataInfo.verifyStatus == '已通过';
				}
			});
			getConfig({}).then((res) => {
				if (res.code == 200) {
					Object.assign(configData, res.data);
				}
			});
		}
	},
	{ immediate: true, deep: true }
);

// 活动操作
const handleRemove = async () => {
	try {
		const res = await showConfirm('是否要删除当前活动?');
		if (res.confirm) {
			const result = await removeActivity({ id: props.dataInfo.id });
			if (result.code === 200) {
				toast('已删除');
				setTimeout(() => {
					uni.navigateBack();
					uni.$emit('changeGetList');
				}, 1000);
			}
		}
	} catch (error) {
		console.error('删除活动失败:', error);
	}
};
const govip = () => {
	uni.navigateTo({
		url: '/mypage/member/index'
	});
	popupRef.value.close();
};
const handleBAOMING = async () => {
	if (vip.value == 0) {
		popupRef.value.open();
		return;
	}
	try {
		const res = await activitySignUp({ id: props.dataInfo.id });
		if (res.code === 200) {
			toast('已报名');
			emit('refesh_info');
			popupYes.value.open();
		}
	} catch (error) {
		console.error('报名失败:', error);
	}
};

const handleHOUBUBAOMING = async () => {
	try {
		const res = await activitySignHb({ id: props.dataInfo.id });
		if (res.code === 200) {
			toast('候补已报名');
			emit('refesh_info');
		}
	} catch (error) {
		console.error('候补报名失败:', error);
	}
};

const handleQUXIAO = async () => {
	try {
		const res = await activitySignCancel({ id: props.dataInfo.id });
		if (res.code === 200) {
			toast('报名已取消');
			emit('refesh_info');
			close();
		}
	} catch (error) {
		console.error('取消报名失败:', error);
	}
};

// 主持人相关
const applyZHUCHI = async () => {
	try {
		const res = await activitySignEmceeApply({ id: props.dataInfo.id });
		if (res.code === 200) {
			toast('已成为主持人');
			emit('refesh_info');
			close();
		}
	} catch (error) {
		console.error('申请主持人失败:', error);
	}
};

const closeZHUCHI = async () => {
	try {
		const res = await activitySignEmceeCancel({ id: props.dataInfo.id });
		if (res.code === 200) {
			toast('已取消');
			emit('refesh_info');
		}
	} catch (error) {
		console.error('取消主持人失败:', error);
	}
};
async function handleSignIn() {
	try {
		// 1. 检查当前的授权状态
		const setting = await new Promise((resolve, reject) => {
			uni.getSetting({
				success: resolve,
				fail: reject
			});
		});

		// 2. 精确检查权限状态
		const locationAuth = setting.authSetting['scope.userLocation'];
		console.log('当前位置权限状态:', locationAuth);

		if (locationAuth === undefined) {
			// 从未询问过授权，发起授权请求
			// 这里会使用您在 permission 中配置的 desc 描述
			console.log('首次请求位置权限');
			try {
				await new Promise((resolve, reject) => {
					uni.authorize({
						scope: 'scope.userLocation',
						success: resolve,
						fail: reject
					});
				});
				console.log('用户授权成功');
			} catch (authError) {
				console.log('用户拒绝授权:', authError);
				// 用户拒绝授权，引导用户打开设置页面
				await handleAuthRejection();
				return;
			}
		} else if (locationAuth === false) {
			// 用户之前明确拒绝过授权，直接引导去设置
			console.log('用户之前已拒绝授权');
			await handleAuthRejection();
			return;
		} else {
			// locationAuth === true 表示已授权
			console.log('用户已授权位置权限');
		}
		// 3. 已获得授权，获取位置并调用接口
		await getLocationAndSignIn();
	} catch (error) {
		console.error('签到流程失败:', error);
		uni.showToast({
			title: '签到失败，请重试',
			icon: 'none'
		});
	}
}
// 处理授权拒绝
async function handleAuthRejection() {
	const confirmResult = await new Promise((resolve) => {
		uni.showModal({
			title: '位置权限申请',
			content: '签到需要获取您的位置信息，请在设置页面中开启位置权限',
			confirmText: '去设置',
			cancelText: '取消',
			success: resolve
		});
	});

	if (confirmResult.confirm) {
		try {
			const openSettingResult = await new Promise((resolve, reject) => {
				uni.openSetting({
					success: resolve,
					fail: reject
				});
			});

			if (openSettingResult.authSetting['scope.userLocation']) {
				// 用户在设置页面开启了位置权限
				uni.showToast({
					title: '授权成功',
					icon: 'success'
				});
				await getLocationAndSignIn();
			} else {
				uni.showToast({
					title: '需要位置权限才能签到',
					icon: 'none'
				});
			}
		} catch (openSettingError) {
			console.error('打开设置页面失败:', openSettingError);
			uni.showToast({
				title: '打开设置失败',
				icon: 'none'
			});
		}
	}
}

// 提取出的获取位置和签到函数
const getLocationAndSignIn = async () => {
	try {
		// 获取用户位置[citation:2]
		const location = await new Promise((resolve, reject) => {
			uni.getLocation({
				type: 'wgs84',
				altitude: true,
				success: resolve,
				fail: reject
			});
		});
		console.log(location);
		// // 调用签到接口
		const res = await activitySignIn({
			id: props.dataInfo.id,
			longitude: location.longitude,
			latitude: location.latitude
		});

		if (res.code === 200) {
			// 签到成功
			popupQIANDAO.value.open();
			emit('refesh_info');
		} else {
			// 签到失败
			popupQIANDAOSHIBAI.value.open();
		}
	} catch (locationError) {
		console.error('获取位置失败:', locationError);
		popupQIANDAOSHIBAI.value.open();
	}
};

const fenshe = reactive({
	menuOptions: [
		// 根据实际需求填写菜单选项
		{ label: '延迟时间', value: 'delay_time', isshow: false }, // 唯一值：延迟时间
		{ label: '更新群码', value: 'update_group_code', isshow: false }, // 唯一值：更新群码
		{ label: '报名成员', value: 'enrolled_members', isshow: false }, // 唯一值：报名成员
		{ label: '上传合影', value: 'upload_photo', isshow: false }, // 唯一值：上传合影
		{ label: '复制活动', value: 'export_activity', isshow: false },
		{ label: '修改地址', value: 'edit_address', isshow: false }
	],
	isshowBtn: false,
	shenhe: {
		id: 0,
		verifyStatus: '',
		verifyDesc: ''
	},
	timeObj: {
		id: '',
		beginTime: null
	}
});

const handleMenuSelect = (value) => {
	// 根据不同value处理逻辑
	switch (value) {
		case 'delay_time':
			console.log('处理延迟时间逻辑');
			// 调用延迟时间相关方法
			popupTime.value.open();
			break;
		case 'update_group_code':
			console.log('处理更新群码逻辑');
			// 调用更新群码相关方法
			popupImg.value.open();
			break;
		case 'enrolled_members':
			console.log('处理报名成员逻辑');
			// 调用查看报名成员相关方法
			uni.navigateTo({
				url: `/mypage/usertabbar/registration?id=${props.dataInfo.id}`
			});
			break;
		case 'upload_photo':
			console.log('处理上传合影逻辑');
			// 调用上传合影相关方法
			popupPhoto.value.open();
			break;
		case 'export_activity':
			console.log('处理导出活动逻辑');
			// 复制活动内容
			const textToCopy =
				'星期：' +
				proxy.$formatTime(props.dataInfo.beginTime, 'week-day') +
				'\n' +
				'日期：' +
				proxy.$formatTime(props.dataInfo.beginTime, 'month-day') +
				'\n' +
				'活动地点所在区县：' +
				props.dataInfo.addressInfo +
				'\n' +
				'活动类型：' +
				props.dataInfo.type +
				'\n' +
				'活动名称：' +
				props.dataInfo.name;
			uni.setClipboardData({
				data: textToCopy,
				success: function () {
					toast('活动内容已复制');
				},
				fail: function (err) {
					console.error('复制失败:', err);
				}
			});
			break;
		case 'edit_address':
			popupAddress.value.open();
			break;
		default:
			break;
	}
};
// 审核相关
const radioChange = (e) => {
	fenshe.shenhe.verifyStatus = e.detail.value;
};
const handleSHENHE = async () => {
	if (!fenshe.shenhe.verifyStatus) {
		toast('请选择审核结果');
		return;
	}
	if (fenshe.shenhe.verifyStatus === '已驳回' && !fenshe.shenhe.verifyDesc) {
		toast('请输入驳回原因');
		return;
	}

	fenshe.shenhe.id = props.dataInfo.id;
	try {
		const res = await groupVerify(fenshe.shenhe);
		if (res.code === 200) {
			toast('已审核');
			emit('refesh_info');
			close();
		}
	} catch (error) {
		console.error('审核失败:', error);
	}
};
// 延迟时间
const handleRTime = () => {
	fenshe.timeObj.id = props.dataInfo.id;
	if (fenshe.timeObj.beginTime == '') {
		toast('请选择时间');
		return;
	}
	groupDelayTime(fenshe.timeObj).then((res) => {
		if (res.code == 200) {
			toast('已延迟活动时间');
			emit('refesh_info');
			close();
		}
	});
};
// 更新群吗
const chooseFENGMIAN = async (e) => {
	try {
		if (e == 1) {
			let imgurl = await chooseAndUploadImage({
				count: 1,
				uploadText: '上传中...'
			});
			if (imgurl) {
				groupUpdateImgQr({
					id: props.dataInfo.id,
					imgQr: imgurl
				}).then((res) => {
					if (res.code == 200) {
						toast('已更换群码');
						emit('refesh_info');
					}
				});
			}
		}
		if (e == 2) {
			let imgurl = await chooseAndUploadImage({
				count: 1,
				uploadText: '上传中...'
			});
			if (imgurl) {
				groupUpdateEndImg({
					id: props.dataInfo.id,
					endImg: imgurl
				}).then((res) => {
					if (res.code == 200) {
						toast('已上传合照');
						emit('refesh_info');
					}
				});
			}
		}
	} catch (err) {
		console.error('头像上传失败：', err);
	}
};
// 申请推荐
const shenqingObj = reactive({
	id: 0,
	hotApplyDesc: ''
});
// 推荐申请
const handleTUIJIAN = () => {
	if (shenqingObj.hotApplyDesc === '') {
		toast('请输入推荐原因');
		return;
	}
	shenqingObj.id = props.dataInfo.id;
	groupApplyHot(shenqingObj).then((res) => {
		if (res.code === 200) {
			toast('已申请');
			emit('refesh_info');
			close();
		}
	});
};
const addressObj = reactive({
	id: 0,
	address: '',
	addressInfo: '',
	longitude: '',
	latitude: ''
});
const selectDW = () => {
	uni.chooseLocation({
		latitude: props.dataInfo.latitude,
		longitude: props.dataInfo.longitude,
		success: function (res) {
			addressObj.address = res.name;
			addressObj.addressInfo = res.address;
			addressObj.longitude = res.longitude;
			addressObj.latitude = res.latitude;
		}
	});
};
// 修改地址
const confirmEditAddress = () => {
	addressObj.id = props.dataInfo.id;
	if (addressObj.addressInfo == '' || addressObj.address == '') {
		toast('请选择地址');
		return;
	}
	activityUpdateAddress(addressObj).then((res) => {
		if (res.code == 200) {
			toast('活动地址已修改');
			emit('refesh_info');
			close();
		}
	});
};
// 暴露方法给父组件
// const openzhuchi = () => {
// 	popupZHUCHI.value.open();
// };

// 暴露方法给父组件
const openzhuchi = () => {
	popupZHUCHI.value.open();
};

defineExpose({
	openzhuchi
});
</script>
<style lang="scss">
.bottom_btns_page {
	width: 750rpx;
	min-height: 120rpx;
	background: #f7f7f7;
	position: fixed;
	bottom: 0%;
	padding-bottom: constant(safe-area-inset-bottom);
	padding-bottom: env(safe-area-inset-bottom);
	z-index: 9;
	.ttt {
		width: 750rpx;
		background: #892b1b;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		opacity: 0.8;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #ffffff;
		line-height: 1.5;
		padding: 15rpx 20rpx;
		box-sizing: border-box;
	}
	.ddd {
		width: 100%;
		line-height: 1.5;
		padding: 15rpx 20rpx;
		background: #b19561;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #ffffff;
	}
	.cont {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		min-height: 120rpx;
		color: #000000;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		padding: 0rpx 20rpx;
		align-items: center;
		.cont_left {
			.itemimg {
				position: relative;
				width: 60rpx;
				text-align: center;
				margin-right: 30rpx;
				.zf_btns {
					width: 60rpx;
					height: 60rpx;
					position: absolute;
					top: 0%;
					opacity: 0;
				}
				image {
					width: 48rpx;
					height: 48rpx;
				}
				.tt {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #aeaeac;
				}
			}
		}
		.btns {
			width: 420rpx;
			height: 80rpx;
			background: #d8c6a7;
			border-radius: 40rpx 40rpx 40rpx 40rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			color: #75613e;
			text-align: center;
			line-height: 80rpx;
		}
		.btn {
			width: 200rpx;
			height: 80rpx;
			border-radius: 40rpx 40rpx 40rpx 40rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			text-align: center;
			line-height: 80rpx;
		}
		.b1 {
			background: #d9d9d9;
			color: #ffffff;
		}
		.b2 {
			background: #d8c6a7;
			color: #75613e;
		}
	}
}
.input_class {
	color: #d1d1d6;
	font-size: 28rpx;
	font-weight: 400;
}
.globalPopup {
	.subtitle {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #8f8f8b;
		margin-top: 30rpx;
	}
	.block_1 {
		margin-top: 18rpx;
		.tit {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #8f8f8b;
		}
		.input {
			margin-top: 18rpx;
			width: 545rpx;
			border-radius: 12rpx;
			border: 1px solid #dadada;
			padding: 20rpx 24rpx;
			box-sizing: border-box;
			textarea {
				width: 100%;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #1b1b1b;
				height: 100rpx;
				line-height: 1.5;
				box-sizing: border-box;
			}
		}
	}
	.content_s {
		.content_item {
			height: 56rpx;
			margin: 30rpx auto;
			.i_l {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #75613e;
				margin-right: 20rpx;
			}

			.i_r {
				flex: 1;
				height: 56rpx;
				background: #eeeeee;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				input {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #000;
					height: 56rpx;
					line-height: 56rpx;
					padding-left: 18rpx;
				}
			}
			.i_r_scroll {
				flex: 1;
				display: flex;
				align-items: center; // 垂直居中选项
				white-space: nowrap; // 强制不换行
				overflow-x: auto; // 内容溢出时显示横向滚动条
				scrollbar-width: none; // 隐藏滚动条（可选）
				.items {
					display: inline-block;
					height: 56rpx;
					background: #eeeeee;
					border-radius: 12rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #8f8f8b;
					line-height: 56rpx;
					margin-right: 20rpx;
					text-align: center;
					padding: 0rpx 20rpx;
				}
				.actitems {
					background: #eae7dd;
					color: #75613e;
				}
			}
		}
	}
	.time_s {
		margin: 30rpx auto;
		.t1 {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #8f8f8b;
			margin-bottom: 20rpx;
		}
	}
	.shenhe {
		.tit_right {
			margin: 40rpx auto;
			font-size: 28rpx;
		}
		textarea {
			width: 100%;
			height: 160rpx;
			border: 1px solid #e5e5e5;
			border-radius: 4px;
			font-family: PingFang SC, PingFang SC;
			font-size: 28rpx;
			color: #000;
			padding: 10rpx;
			box-sizing: border-box;
			margin: 0rpx auto 30rpx;
		}
	}
	.upload {
		width: 400rpx;
		height: 400rpx;
		background: #f7f7f7;
		border-radius: 20rpx;
		border: 1px solid #dadada;
		margin: 50rpx auto;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #75613e;
		text-align: center;
		position: relative;
		image {
			width: 400rpx;
			height: 400rpx;
			border-radius: 20rpx;
		}
		.ads {
			position: absolute;
			width: 400rpx;
			height: 80rpx;
			background: rgba(0, 0, 0, 0.5);
			border-radius: 0rpx 0rpx 20rpx 20rpx;
			bottom: 0%;
			line-height: 80rpx;
			color: #ffffff;
		}
	}
	.upload1 {
		width: 100%;
		height: 280rpx;
		position: relative;
		margin: 40rpx auto 10rpx;
		border-radius: 20rpx;
		image {
			width: 100%;
			height: 280rpx;
			border-radius: 20rpx;
		}
		.ads {
			position: absolute;
			width: 100%;
			height: 280rpx;
			background: rgba(0, 0, 0, 0.5);
			image {
				width: 88rpx;
				height: 88rpx;
			}
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #ffffff;
			text-align: center;
		}
		.adsts {
			position: absolute;
			font-size: 28rpx;
			width: 100%;
			height: 80rpx;
			background: rgba(0, 0, 0, 0.5);
			border-radius: 0rpx 0rpx 20rpx 20rpx;
			bottom: 0%;
			line-height: 80rpx;
			color: #ffffff;
			text-align: center;
		}
	}
}

.popup {
	width: 620rpx;
	background: linear-gradient(180deg, #ece6d5 0%, #f7f8f6 100%);
	border-radius: 20rpx 20rpx 20rpx 20rpx;
	padding: 32rpx 30rpx;
	box-sizing: border-box;
	text-align: center;
	position: relative;
	.close {
		position: absolute;
		right: 30rpx;
		top: 30rpx;
		display: block;
	}
	.uimg {
		margin-top: 40rpx;
		image {
			width: 140rpx;
			height: 148rpx;
		}
	}

	.title {
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 40rpx;
		color: #4e3f23;
		text-align: center;
		margin: 30rpx auto;
	}
	.subtitle {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #b19561;
		margin: 30rpx auto;
	}
	.img {
		image {
			width: 400rpx;
			height: 400rpx;
			margin-bottom: 30rpx;
			border-radius: 20rpx;
		}
	}

	.title1 {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 32rpx;
		color: #000000;
		margin-top: 20rpx;
		text-align: center;
	}
	.pop_content {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #000000;
		margin: 40rpx auto;
		padding: 0rpx 20rpx;
	}
	.pop_btn {
		display: inline-block;
		height: 72rpx;
		background: #d8c6a7;
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #75613e;
		text-align: center;
		line-height: 72rpx;
		margin: 0 auto;
		padding: 0rpx 30rpx;
	}
	.pop_btnflex {
		margin-top: 30rpx;
	}
	.btn {
		width: 260rpx;
		height: 72rpx;
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		text-align: center;
		line-height: 72rpx;
	}
	.b1 {
		background: #f9f9f9;
		color: #d1cdcd;
	}
	.b2 {
		background: #d8c6a7;
		color: #75613e;
	}
}
.p1 {
	background: #ffffff;
	text-align: left;
	padding: 30rpx 40rpx;
}

.dropdown-menu {
	position: absolute;
	z-index: 999; /* 确保在最上层 */
	will-change: transform; /* 优化渲染性能，减少闪烁 */
	display: flex;
	flex-direction: column;
	align-items: flex-start;
	bottom: 100rpx;
	left: 0%;
}

/* 箭头样式（与菜单无缝衔接） */
.arrow {
	width: 0;
	height: 0;
	border-width: 8px;
	border-style: solid;
	position: absolute;
	border-color: #fff transparent transparent transparent;
	bottom: -16px; /* 与菜单底部衔接 */
	left: 10px;
}
/* 菜单内容样式 */
.menu-content {
	background-color: #fff;
	border-radius: 8px;
	box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
	min-width: 180rpx;
	padding: 15rpx 0; /* 优化内边距 */
	box-sizing: border-box;
}

/* 菜单项样式 */
.menu-item {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 12rpx 20rpx; /* 增大点击区域 */
	white-space: nowrap; /* 防止文字换行 */
	&:active {
		background-color: #f8f8f8; /* 点击反馈 */
	}

	image {
		width: 36rpx;
		height: 36rpx;
		margin-right: 10rpx;
	}

	.texts {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #8c8c8c;
	}
}
</style>
